document.getElementById('askButton').addEventListener('click', async function () {
    const question = document.getElementById('question').value.trim();
    if (!question) {
        alert("请输入一个问题！");
        return;
    }

    const responseDiv = document.getElementById('response');
    responseDiv.style.display = 'none'; // 在请求之前隐藏答案区域

    try {
        // 创建 FormData 对象，传递参数
        const formData = new FormData();
        formData.append('question', question);

        // 发送请求到后端API
        const response = await fetch('http://localhost:8888/test/testAi2', {
            method: 'POST',
            body: formData // 使用 FormData 发送数据
        });

        if (response.ok) {
            const data = await response.json();  // 处理后端返回的数据
            responseDiv.innerHTML = `<strong>回答：</strong> ${data.answer || '未找到答案，请稍后再试。'}`;
            responseDiv.style.display = 'block';
        } else {
            responseDiv.innerHTML = '服务器错误，请稍后重试。';
            responseDiv.style.display = 'block';
        }
    } catch (error) {
        responseDiv.innerHTML = '请求失败，请检查网络连接或稍后再试。';
        responseDiv.style.display = 'block';
    }
});

